﻿using System.Web.UI.WebControls;
using System.Linq;
using MedianamikControls = Medianamik.UI.Controls;
using Medianamik.Core;
using Medianamik.Core.DAL.SubSonicDAL;
using System;

namespace Medianamik.UI.Blog.Controls
{
    public abstract class BaseGroupsDropDownList : MedianamikControls.BaseMedianamikControl
    {
        private DropDownList ddlGroup = new DropDownList { ID = ControlToValidateId };

        public abstract string GroupFilter { get; }

        public override object GetValue()
        {
            return ddlGroup.SelectedValue;
        }

        public override System.Type TypeOfValue
        {
            get { return typeof (string); }
        }

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            var groups = new UserNameInGroupNameCollection();
            groups.Where(UserNameInGroupName.Columns.GroupName, GroupFilter);
            groups.Load();

            ddlGroup.Items.Add(new ListItem());
            ddlGroup.Items.AddRange(groups.Select(g => new ListItem(g.UserName)).ToArray());


            if (!String.IsNullOrEmpty((string)(DataSource ?? "")))
            {
                var item = ddlGroup.Items.FindByValue(DataSource.ToString());
                if (item != null)
                    item.Selected = true;
            }

            Controls.Add(ddlGroup);
        }

        public static void CreateControlInDB()
        {
        }
    }
}
